package cn.car91.activity;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import android.os.Bundle;
import android.widget.TextView;
import cn.car91.R;
import cn.car91.adapter.OilListAdapter;
import cn.car91.adapter.OilListAdapter.Oil;
import cn.car91.utils.RegexUtil;
import cn.car91.view.MyListView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.view.annotation.ViewInject;

public class OilActivity extends BaseActivity {

    @ViewInject(R.id.oil_list)
    MyListView oilListView;
    @ViewInject(R.id.oil_city)
    TextView oilCityView;
    @ViewInject(R.id.oil_0)
    TextView oil0View;
    @ViewInject(R.id.oil_90)
    TextView oil90View;
    @ViewInject(R.id.oil_93)
    TextView oil93View;
    @ViewInject(R.id.oil_97)
    TextView oil97View;

    OilListAdapter oilListAdapter;

    String cityname;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        setContentView(R.layout.activity_oil);
        super.onCreate(savedInstanceState);
        oilListAdapter = new OilListAdapter(this);
        oilListView.setAdapter(oilListAdapter);
        cityname = getIntent().getStringExtra("cname");
        if (RegexUtil.isNull(cityname))
            cityname = "全国";
        initOil();

        ((TextView) findViewById(R.id.header_text)).setText(cityname + "今日油价");
    }

    private void initOil() {
        String resultJson = getCar91Application().getOtherPreferences().getString("oilJson", "");
        if (RegexUtil.isNotNull(resultJson)) {
            JSONObject jsonObject = JSONObject.parseObject(resultJson);
            JSONArray dataObject = jsonObject.getJSONArray("data");
            oilCityView.setText(cityname + "基准油价");
            if (dataObject.size() > 0) {
                JSONObject oilObject = dataObject.getJSONObject(0);
                JSONObject priceObject = oilObject.getJSONObject("price");
                Iterator<Entry<String, Object>> allprice = priceObject.entrySet().iterator();
                while (allprice.hasNext()) {
                    Entry<String, Object> tmp = allprice.next();
                    String tprice = (String) tmp.getValue();
                    if (tmp.getKey().equals("E0"))
                        oil0View.setText(tprice);
                    else if (tmp.getKey().equals("E90") || tmp.getKey().equals("E89"))
                        oil90View.setText(tprice);
                    else if (tmp.getKey().equals("E93") || tmp.getKey().equals("E92"))
                        oil93View.setText(tprice);
                    else if (tmp.getKey().equals("E97") || tmp.getKey().equals("E95"))
                        oil97View.setText(tprice);
                }
            }
            List<Oil> oils = new ArrayList<OilListAdapter.Oil>();
            for (int i = 0; i < dataObject.size(); i++) {
                Oil oil = oilListAdapter.instanceOil();
                JSONObject oilObject = dataObject.getJSONObject(i);
                String oname = oilObject.getString("name");
                String oaddress = oilObject.getString("address");
                oil.name = oname;
                oil.address = oaddress;

                String gastprice = oilObject.getString("gastprice");
                try {
                    gastprice = gastprice.substring(1, gastprice.lastIndexOf("}"));
                    gastprice = gastprice.replace("\"", "");
                    String[] tmp = gastprice.split(",");
                    for (String string : tmp) {
                        String[] ttmp = string.split(":");
                        if (ttmp[0].equals("0#"))
                            oil.oil0 = ttmp[1];
                        else if (ttmp[0].equals("90#") || ttmp[0].equals("89#"))
                            oil.oil90 = ttmp[1];
                        else if (ttmp[0].equals("93#") || ttmp[0].equals("92#"))
                            oil.oil93 = ttmp[1];
                        else if (ttmp[0].equals("97#") || ttmp[0].equals("95#"))
                            oil.oil97 = ttmp[1];
                    }
                    oils.add(oil);
                } catch (Exception e) {
                }
            }
            oilListAdapter.setData(oils);
        }
    }
}
